define(function (require) {
var zsApp= require('app');  
require('selectCache');
zsApp.controller('ipRuleCtrl', ['$scope', '$http', '$timeout', 'selectCache', 'promptCache', function($scope, $http, $timeout, selectCache, promptCache) {	

	// pagination
	$scope.paginationConf = {
        currentPage     : 1,
        totalItems      : 10,
        itemsPerPage    : 20,
        pagesLength     : 15,
        perPageOptions  : [10, 20, 30, 40, 50],
        onChange: function(){
        	var self = this,
        		pagingData = {
	        		pageNumber  : self.currentPage,  
					pageSize 	: self.itemsPerPage,  
	        	};	
			$http
			({
				url  	: $scope.zsUrl+'digitalhome-boss/admin/iprules/getList',
				method 	: 'POST',
				data 	: pagingData,

			}).success(function(res) {

				if(res.result_code != '0000') {
                    $scope.isNoData = false;
                    return false;
                }

				// 系统配置键列表页面表格
				$scope.tabVal = res.result;
				// 遍历每条数据对应的ID
				angular.forEach($scope.tabVal, function(obj, i) {
					$scope.dataID[i] = obj.ipRuleId;
				});
				// 总共有多少条数据
				self.totalItems = res.length;


			}).error(function(err) {
				console.log(err);
			});
        }
    };

    // submitNewlyAdded
    $scope.submitNewlyAdded = function() {

    	var newlyAddedData = {
    		ruleName 	: $scope.newlyAdded_ruleName,
    		sortIndex 	: $scope.newlyAdded_sortIndex,
    		ruleMode 	: $scope.newlyAdded_ruleMode,
    		enabled 	: $scope.newlyAdded_enabled,
    		expression 	: $scope.newlyAdded_expression,
    	};

    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/admin/iprules/addIprules',
    		method	: 'POST',
    		data 	: newlyAddedData,
    	})
    	.success(function(res) {
    		
            if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();

    	})
    	.error(function(err) {
    		promptCache.errorPrompt('新增失败！');
            return false;
    	});
    }

    // details
    $scope.details = function(i) {
    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/admin/iprules/getIprulesInfo/'+$scope.dataID[i],
    		method	: 'GET',

    	}).success(function(res) {	

    		if( '0000' != res.result_code ) {
    			return false;
    		}

    		var obj = res.result;

    		$scope.details_ruleName 	= obj.ruleName;
    		$scope.details_sortIndex 	= obj.sortIndex;
    		$scope.details_ruleMode    	= obj.ruleMode;
    		$scope.details_enabled		= obj.enabled;
    		$scope.details_userId 		= obj.userId;
    		$scope.details_expression 	= obj.expression;

    	}).error(function(err) {
    		console.log(err);
    	});
    }

    // edit: show data
    $scope.edit = function(i) { 

    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/admin/iprules/getIprulesInfo/'+$scope.dataID[i],
    		method	: 'GET',

    	}).success(function(res) {

			if( '0000' != res.result_code ) {
    			return false;
    		}

    		$scope.index = i;

    		var obj = res.result;

			$scope.edit_ruleName 	= obj.ruleName;
    		$scope.edit_sortIndex 	= obj.sortIndex;
    		$scope.edit_ruleMode    = obj.ruleMode;
    		$scope.edit_enabled		= obj.enabled;
    		$scope.edit_expression 	= obj.expression;

    		$scope.editSortSelect = 
    		[
    			{ 'id' : 0, 'name': '高' },
    			{ 'id' : 1, 'name': '中' },
    			{ 'id' : 2, 'name': '低' },
    		];

    		$scope.editRuleModeSelect = 
    		[
    			{ 'id' : 0, 'name': '禁止' },
    			{ 'id' : 1, 'name': '允许' },
    		];

    		$scope.editEnabledSelect = 
    		[
    			{ 'id' : 0, 'name': '禁用' },
    			{ 'id' : 1, 'name': '启用' },
    		];

    	}).error(function(err) {
    		console.log(err);
    	});
    };

    // edit: submit data
    $scope.confirmEdit = function() {

    	var editData = {
    		ipRuleId	: $scope.dataID[$scope.index],
    		ruleName 	: $scope.edit_ruleName,
    		sortIndex 	: $scope.edit_sortIndex,
    		ruleMode 	: $scope.edit_ruleMode,
    		enabled 	: $scope.edit_enabled,
    		expression 	: $scope.edit_expression,
    	};

    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/admin/iprules/modify',
    		method	: 'POST',
    		data 	: editData,

    	}).success(function(res) {
    		
            if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();  
    		
    	}).error(function(err) {
    		promptCache.errorPrompt('修改失败！');
            return false;
    	});
    }

    // delete
    $scope.getDataID = function() {

    	// data length
    	var len = $scope.dataID.length;
    	// param id
    	var whichId = 'ipRuleId';
    	// param
    	var whichUrl = 'admin/iprules/delete';

    	// multiple selections 
        $scope.selectAll= function (bool, obj) {
            selectCache.selectAll(bool, $scope.dataID);
        };

        //  Single or multiple selections 
        $scope.select= function (selectObj, bool) {   
         	var selectId = selectObj.ipRuleId;        
            selectCache.select(selectId, bool, len);    
        }; 

        // check
        $scope.check= function () {
            selectCache.check();
        };

        // delete
        $scope.delete= function () { 
            selectCache.delete(whichId, whichUrl);
            reloadTable();
        };
    };

    // reload table
    function reloadTable() { 
        $timeout(function() {
            $scope.paginationConf.onChange();
            $scope.master = false;
        },1500);
    };
}])
.directive('ipNewlyadded', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/ip-rule/newlyAdded.html',
		replace			: true,
	}
})
.directive('ipDetails', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/ip-rule/details.html',
		replace			: true,
	}
})
.directive('ipEdit', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/ip-rule/edit.html',
		replace			: true,
	}
})
.directive('ipDelete', function() {
    return {
        restrict        : 'E',
        templateUrl     : 'system/openPlatform/ip-rule/delete.html',
        replace         : true,
    }
})
.directive('ipTable', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/ip-rule/table.html',
		replace			: true,
	}
});
});